﻿Imports System.Data.SqlClient

Public Class frmQuanLyThuNgan
    Shared hoadon As New CHoaDon
    Dim startMaHoaDon As Integer = 1000

    Dim dsBan As New List(Of CBan)
    Dim da As DataProvider
    Dim ban As New CBan



    Private Sub frmQuanLyThuNgan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        LoadComboboxKhuVuc()

        LoadMaNV()
        ban.LoadDanhSachBan(dsBan)
        tvChonBan.Nodes.Clear()
        LoadTreeView(cbxKhuVuc.SelectedValue)
        tvChonBan.ExpandAll()
    End Sub
    Public Sub LoadMaNV()
        da = New DataProvider
        Dim dt As New DataTable
        da.OpenConnect()
        cbxMaNVPV.DataSource = da.ShowData("select * from NHANVIEN")
        cbxMaNVPV.DisplayMember = "MaNhanVien"
        cbxMaNVPV.ValueMember = "TenNhanVien"
        da.CloseConnect()
    End Sub
    Public Sub LoadMaHoaDon()
        da.OpenConnect()
        Dim sql As String = "select MaHoaDon from HOADON"
        Dim cmd As New SqlCommand(sql, da.con)
        If da.GetData(sql) = "" Then
            txtMaHoaDon.Text = startMaHoaDon.ToString()
        Else
            Dim max As New Integer
            da.OpenConnect()
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            While dr.Read()
                If dr(0) > max Then
                    max = dr(0)
                End If
            End While
            txtMaHoaDon.Text = (max + 1).ToString()
        End If
    End Sub
    Public Sub LoadTreeView(ByVal makv As String)
        Dim BanCoKhach, BanTrong As New TreeNode

        BanTrong.Name = "bantrong"
        BanTrong.Text = "Bàn trống"
        BanCoKhach.Name = "bancokhach"
        BanCoKhach.Text = "Bàn có khách"

        tvChonBan.Nodes.Add(BanCoKhach)
        tvChonBan.Nodes.Add(BanTrong)

        For i = 0 To dsBan.Count - 1
            Dim node As New TreeNode
            If dsBan.Item(i).KhuVuc = makv Then
                If dsBan.Item(i).TinhTrang = 1 Then
                    node.Name = dsBan.Item(i).MaBan
                    node.Text = dsBan.Item(i).TenBan
                    BanCoKhach.Nodes.Add(node)
                Else
                    node.Name = dsBan.Item(i).MaBan
                    node.Text = dsBan.Item(i).TenBan
                    BanTrong.Nodes.Add(node)
                End If
            End If
        Next
    End Sub

    Public Sub LoadComboboxKhuVuc()
        da = New DataProvider
        Dim dt As New DataTable
        da.OpenConnect()
        cbxKhuVuc.DataSource = da.ShowData("select * from KHUVUC")
        cbxKhuVuc.DisplayMember = "TenKhuVuc"
        cbxKhuVuc.ValueMember = "MaKhuVuc"
        da.CloseConnect()
    End Sub


    Private Sub cbxKhuVuc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxKhuVuc.SelectedIndexChanged
        tvChonBan.Nodes.Clear()
        LoadTreeView(cbxKhuVuc.SelectedValue)
        tvChonBan.ExpandAll()
    End Sub

    Private Sub btnGoiMon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGoiMon.Click
        Dim node = tvChonBan.SelectedNode
        If node Is Nothing Then
            MsgBox("Hãy chọn một bàn", MsgBoxStyle.OkOnly, "Thông báo")
            Return
        Else
            tvChonBan.Nodes(0).Nodes.Add(node.Text)
            tvChonBan.Nodes(1).Nodes.Remove(node)
        End If

        frmThucDon.Show()

       

    End Sub

    Private Sub btnDonBan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDonBan.Click
        Try
            UpdateTinhTrang(txtMaBan.Text, 0)
            tvChonBan.Nodes.Clear()
            dsBan.Clear()
            ban.LoadDanhSachBan(dsBan)
            LoadTreeView(cbxKhuVuc.SelectedValue)
            tvChonBan.ExpandAll()
            XoaTTManHinh()

        Catch ex As Exception
            MsgBox("Hãy chọn một bàn", MsgBoxStyle.OkOnly, "Thông báo")
        End Try

    End Sub
    Private Sub tvChonBan_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvChonBan.AfterSelect
        'Try

        '    If tvChonBan.SelectedNode.Parent.Name = "bantrong" Then
        '        txtMaBan.Text = tvChonBan.SelectedNode.Name
        '        LoadMaHoaDon()
        '    Else
        '        XoaTTManHinh()
        '        txtMaBan.Text = tvChonBan.SelectedNode.Name
        '        MessageBox.Show("Ban vua chon node ban co khach")
        '    End If
        'Catch ex As Exception
        '    Return
        'End Try


        If tvChonBan.SelectedNode.Parent.Name = "bantrong" Then
            txtMaBan.Text = tvChonBan.SelectedNode.Name
            LoadMaHoaDon()
        ElseIf tvChonBan.SelectedNode.Parent.Name = "banCoKhach" Then
            Dim sql = "select ct.MaHoaDon, ct.MaMonAn, ct.NgayLap, ct.SoLuong, ct.DonGia from HOADON hd, " + _
                "CTHOADON ct where hd.MaBan = '" + tvChonBan.SelectedNode.Name + "' and hd.MaHoaDon = ct.MaHoaDon"
            Dim cmd As New SqlCommand(sql, da.con)
            Dim reader As SqlDataReader = cmd.ExecuteReader()
            While reader.Read()
                Dim item As New ListViewItem(reader.Item(0).ToString())
                item.SubItems(1).Text = reader.Item(1).ToString()
                item.SubItems(2).Text = reader.Item(2).ToString()
                item.SubItems(3).Text = reader.Item(3).ToString()
                item.SubItems(4).Text = reader.Item(4).ToString()
                lstTTSanPham.Items.Add(item)
            End While

        End If

    End Sub

    Private Sub btnLuuHoaDon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuuHoaDon.Click
        hoadon.MaHoaDon = Integer.Parse(txtMaHoaDon.Text)
        hoadon.NgayNhap = DateTime.Parse(DateTime.Today.Date)
        hoadon.MaNhanVien = Integer.Parse(cbxMaNVPV.Text)
        hoadon.MaBan = txtMaBan.Text

        hoadon.InsertHoaDon(hoadon)

        'insert chi tiet hoa don
        Dim monan As New CCTHoaDon

        For i = 0 To lstTTSanPham.Items.Count() - 1
            monan.SoLuong = Integer.Parse(lstTTSanPham.Items(i).SubItems(1).Text)
            monan.NgayLap = DateTime.Now.Day.ToString()
            monan.MaMonAn = Integer.Parse(lstTTSanPham.Items(i).Name)
            monan.MaHoaDon = txtMaHoaDon.Text
            'lstTTSanPham.Items(i).SubItems(1)
        Next

        UpdateTinhTrang(txtMaBan.Text, 1)
        tvChonBan.Nodes.Clear()
        dsBan.Clear()
        ban.LoadDanhSachBan(dsBan)
        LoadTreeView(cbxKhuVuc.SelectedValue)
        tvChonBan.ExpandAll()
        XoaTTManHinh()
    End Sub
    Public Sub UpdateTinhTrang(ByVal maban As String, ByVal state As Integer)
        Dim sql As String = "UPDATE BAN SET TinhTrang = " + state.ToString() + " WHERE MaBan = '" + maban + "'"
        CKiemTra.Update(sql)
    End Sub
    Public Sub XoaTTManHinh()
        txtMaBan.Text = ""
        txtMaHoaDon.Text = ""
        txtTongTien.Text = ""
    End Sub
    Public Sub TongTien()
        If lstTTSanPham Is Nothing Then
            Return
        Else
            Dim s As New Integer
            s = 0
            Dim soluong, dongia As Integer
            For i = 0 To lstTTSanPham.Items.Count - 1
                soluong = Integer.Parse(lstTTSanPham.Items(i).SubItems(1).Text)
                dongia = Integer.Parse(lstTTSanPham.Items(i).SubItems(2).Text)
                s = s + soluong * dongia
            Next
            txtTongTien.Text = s.ToString()
        End If
    End Sub

    Private Sub btnHuyBan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuyBan.Click

    End Sub

    Private Sub btnInHoaDon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInHoaDon.Click
        'check rb da thanh toan
        ' luu vao csdl
    End Sub
End Class

